#include "MyDirectX.h"
const string apptitle = "Bomb Catcher Game";
const int SCREENW = 1024;
const int SCREENH = 768;
LPDIRECT3DSURFACE9 bomb_surf = NULL;
LPDIRECT3DSURFACE9 bucket_surf = NULL;

struct BOMB
{
	float x, y;
	void reset() 
	{
		x = (float)(rand() % (SCREENW - 128));
		y = 0;
	}
};

BOMB bomb;

struct BUKET
{
	float x, y;

};

BUKET bucket;
int score = 0;
int vibration = 0;
bool Game_Init(HWND window)
{
	Direct3D_Init(window, SCREENW, SCREENH, false);
	DirectInput_Init(window);
	bomb_surf = loadSurface("bomb.bmp");
	if (!bomb_surf) {
		MessageBox(window,"Error loading bomb","ERROR",MB_OK);
		return false;
	}
	bucket_surf = loadSurface("bucket.bmp");
	if (!bucket_surf) {
		MessageBox(window, "Error Loading buket", "Error", MB_OK);
		return false;
	}

	//get the back buffer surface
	d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
	srand((unsigned int)time(NULL));
	bomb.reset();
	bucket.x = 500;
	bucket.y = 630;
	return true;
}

void Game_Run(HWND window)
{
	if (!d3ddev)
	{
		return;
	}
	DirectInput_Update();
	bomb.y += 2.0f;

	if (bomb.y > SCREENH)
	{
		MessageBox(0, "On no, the bomb exploded!!", "You StINK", MB_OK);
		gameover = true;
	}
	//move the bucket with keyboard
	int mx = Mouse_X();
	if (mx < 0)
	{
		bucket.x -= 6.0f;
	}
	else if (mx > 0) 
	{
		bucket.x += 6.0f;
	}
	//move the bucket with keyboard
	if (Key_Down(DIK_LEFT))
	{
		bucket.x -= 6.0f;
	}
	else if (Key_Down(DIK_RIGHT))
	{
		bucket.x += 6.0f;
	}

	if (XInput_Controller_Found()) {
		//left analog thumb stick
		if (controller[0].sThumbLX < -5000)
			bucket.x -= 6.0f;
		else if (controller[0].sThumbLX > 5000)
			bucket.x += 6.0f;
		//left and right trigger
		if (controller[0].bLeftTrigger > 128)
			bucket.x -= 6.0f;
	}

	if (vibration > 0) {
		vibration++;
		if (vibration > 20) {
			XInput_Vibrate(0, 0);
			vibration = 0;
		}
	}

	//keep bucket inside the screen
	if (bucket.x < 0)
		bucket.x = 0;
	if (bucket.x > SCREENW - 128)
		bucket.x = SCREENW - 128;
	//see if bucket saught the bomb
	int cx = bomb.x + 64;
	int cy = bomb.y + 64;

	if (cx > bucket.x && cx < bucket.x + 128 && cy > bucket.y && cy < bucket.y + 128) {
		//update and display score
		score++;
		std::ostringstream os;
		os << apptitle << "[SCORE " << score << "]";
		string scoreStr = os.str();
		SetWindowText(window, scoreStr.c_str());
		//vibrate the controller
		XInput_Vibrate(0, 65000);
		vibration = 1;
		//restart bomb
		bomb.reset();
	}

	d3ddev->ColorFill(backbuffer, NULL, D3DCOLOR_XRGB(0, 0, 0));
	//start rendering
	if (d3ddev->BeginScene()) {
		//draw the bomb
		DrawSurface(backbuffer, bomb.x, bomb.y, bomb_surf);
		//draw the bucket;
		DrawSurface(backbuffer, bucket.x, bucket.y, bucket_surf);

		//stop rendering
		d3ddev->EndScene();
		d3ddev->Present(NULL, NULL, NULL, NULL);
	}

	//escape bey exits
	if (Key_Down(DIK_SPACE) || Key_Down(DIK_ESCAPE)) {
		gameover = true;
	}

	if (controller[0].wButtons & XINPUT_GAMEPAD_BACK) {
		gameover = true;
	}

}

void Game_End()
{
	if (bomb_surf)
		bomb_surf->Release();
	if (bucket_surf)
		bucket_surf->Release();
	DirectInput_Shutdown();
	Direct3D_Shutdown();
}
